package com.bdmc.web.controller.api.http;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.bdmc.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WatchHttp {
    public static Boolean sendCircleFence(String deviceId,String longitude,String latitude,int fanwei,String name){
        String url = "http://yg.hxinwatch.com/sdkapi/api/push/hjk/updateelectricfence.htm";
        String apiUrl = url + "?deviceid=" + deviceId + "&longitude=" + longitude+ "&latitude=" + latitude+"&fanwei=" + fanwei+"&name=" + name;
        String response = HttpUtil.get(apiUrl);
        // 解析返回的JSON数据o;
        JSONObject jsonObject = new JSONObject(response);
        String msg = jsonObject.getStr("msg");
        int code = jsonObject.getInt("code");
        // 判断请求是否成功
        if (code == 200) {
            log.info("请求成功！返回结果：" + response);
            return true;
        } else {
            log.info("请求失败！错误消息：" + msg);
            return false;
        }
    }

    /**
     * 设置多边形围栏
     * @param deviceId 设备id
     * @param name 名称
     * @param jsonArray 数组
     * @return 结果
     */
    public static Boolean sendPolygonFence(String deviceId, String name, JSONArray jsonArray){
        String url = "http://yg.hxinwatch.com/sdkapi/api/push/hjk/updateelectricfence.htm";
        JSONObject args = new JSONObject();
        args.set("path",jsonArray);
        args.set("name",name);
        args.set("deviceid",deviceId);
        String response = HttpUtil.post(url,args);
        // 解析返回的JSON数据
        JSONObject jsonObject = new JSONObject(response);
        String msg = jsonObject.getStr("msg");
        int code = jsonObject.getInt("code");
        // 判断请求是否成功
        if (code == 200) {
            log.info("请求成功！返回结果：" + response);
            return true;
        } else {
            log.info("请求失败！错误消息：" + msg);
            return false;
        }
    }

    //发送指令（数据）到设备
    public static Boolean sendData(String deviceId,int cmd,String cmdalue,String cmdvaluex){
        String apiUrl = "http://yg.hxinwatch.com/sdkapi/api/push/hjk/submitcommand.htm";
        // 调用请求方法
        String response = sendHttpRequest(apiUrl, deviceId, cmd,cmdalue,cmdvaluex);
        // 解析返回的JSON数据
        JSONObject jsonObject = new JSONObject(response);
        String msg = jsonObject.getStr("msg");
        int code = jsonObject.getInt("code");

        // 判断请求是否成功
        if (code == 200) {
            log.info("请求成功！返回结果：" + response);
            return true;
        } else {
            log.info("请求失败！错误消息：" + msg);
            return false;
        }
    }



    // 封装HTTP请求方法
    private static String sendHttpRequest(String url, String deviceId, int command,String cmdvalue,String cmdvaluex) {
        String apiUrl = url + "?deviceid=" + deviceId + "&command=" + command;
        if(StringUtils.isNotEmpty(cmdvalue)){
            apiUrl += "&cmdvalue="+cmdvalue;
        }
        if(StringUtils.isNotEmpty(cmdvaluex)){
            apiUrl += "&cmdvaluex="+cmdvaluex;
        }
        return HttpUtil.get(apiUrl);
    }
}
